package com.controller;

import com.bean.Evaluate;
import com.bean.OrderItem;
import com.google.gson.Gson;
import com.service.EvaluateService;
import com.service.OrderItemService;
import com.service.impl.EvaluateServiceImpl;
import com.service.impl.OrderItemServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EvaluateServlet extends BaseServlet {

    private EvaluateService evaluateService=new EvaluateServiceImpl();
    private OrderItemService orderItemService=new OrderItemServiceImpl();

    /**
     * 用户对象鲜花产品进行评价
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void useraddEvaluate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("过来了！1");
       // String oid=req.getParameter("id");
       // Integer id=Integer.parseInt(oid);
        Evaluate evaluate= WebUtils.copyParamToBean(req.getParameterMap(),new Evaluate());
        Integer id=evaluate.getOrderitemid();
        try {
            evaluateService.useraddEvaluate(evaluate);
            System.out.println(evaluate);
            req.setAttribute("mess","已完成评价！");
            System.out.println("已完成评价");
            orderItemService.updateStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/page/user/index.jsp").forward(req,resp);
    }

    /**
     * 查看我的评价（用户、企业）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryMyEvaluate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id=req.getParameter("id");

        try {
            List<Map<String,Object>>  evaluates=evaluateService.listMyEvaluate(id);
            System.out.println(evaluates);
            req.setAttribute("evaluates",evaluates);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/page/user/myEvaluate.jsp").forward(req,resp);
    }

    /**
     * 商家查看评价
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryEvaluates(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        List<Evaluate> evaluates=null;
        try {
            evaluates=evaluateService.queryMyEvaluate(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(evaluates);
        req.setAttribute("evaluates",evaluates);
        req.getRequestDispatcher("/page/enterprise/myEvaluate.jsp").forward(req,resp);
    }

    /**
     * 根据鲜花id来查看评价
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showEvaluate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String fid=req.getParameter("flowerid");
        Integer flowerid=Integer.parseInt(fid);
        List<Evaluate> evaluates=null;
        evaluates=evaluateService.queryByFlowerid(flowerid);
        System.out.println(evaluates);
        Map<String,List> result=new HashMap<String,List>();
        result.put("evaluates",evaluates);
        Gson gson=new Gson();
        String json=gson.toJson(result);
        System.out.println(json);
        resp.getWriter().write(json);
    }
}
